<?php

namespace app\modules\customer\controllers;

use app\common\service\UrlService;
use app\common\service\UtilService;
use app\models\book\Book;
use app\modules\customer\common\BaseController;
use app\models\brand\BrandSetting;
use app\models\brand\BrandImages;


/**
 * Default controller for the `customer` module
 */
class DefaultController extends BaseController
{
    //http://www.17sucai.com/pins/22261.html
    public function actionIndex()
    {
        $info = BrandSetting::find()->one();
        $image_list = BrandImages::find()->all();

        $list = $this->getData();

        $data = [];
        if ($list) {
            foreach ($list as $_item) {
                $data[] = [
                    'id' => $_item['id'],
                    'name' => UtilService::encode($_item['name']),
                    'price' => UtilService::encode($_item['price']),
                    'main_image_url' => UrlService::buildPicUrl("book", $_item['main_image']),
                    'month_count' => $_item['month_count']
                ];
            }
        }

        return $this->render('index', [
            'info' => $info,
            'image_list' => $image_list,
            'list' => $data
        ]);
    }

    private function getData($page_size = 4)
    {

        $query = Book::find()->where(['status' => 1]);

        $query->orderBy(['month_count' => SORT_DESC]);

        return $query->limit($page_size)->all();
    }

    public function actionShared()
    {
//        $url = $this->post("url", "");
//        if (!$url) {
//            $url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
//        }
//
//        $model_wx_shared = new WxShareHistory();
//        $model_wx_shared->member_id = $this->current_user ? $this->current_user['id'] : 0;
//        $model_wx_shared->share_url = $url;
//        $model_wx_shared->created_time = date("Y-m-d H:i:s");
//        $model_wx_shared->save(0);
//        return $this->renderJSON([]);
    }
}
